package com.lucian.ge.user.controller;

import com.alibaba.fastjson.JSON;
import com.lucian.ge.user.mapper.UserMapper;
import com.lucian.ge.comman.model.ResultMap;
import com.lucian.ge.user.model.User;
import com.lucian.ge.util.JWTUtil;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.DigestUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;


/**
 * Created by Lucicol on 2019/5/6.
 */
@Api(value = "LoginController", description = "普通用户登录接口")
@RestController
public class LoginController {
    private final UserMapper userMapper;
    private final ResultMap resultMap;

    @Autowired
    public LoginController(UserMapper userMapper, ResultMap resultMap) {
        this.userMapper = userMapper;
        this.resultMap = resultMap;
    }

    @ApiOperation(value="用户登录", notes="手机号/密码 {username,password}")

    @ApiImplicitParams({
//        @ApiImplicitParam(name = "map", required = true , paramType = "body")
    })

    @PostMapping("/login")
    @ResponseBody
    public ResultMap login(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) @Valid User user, BindingResult result) {
//        @RequestBody Map<String, String> map
//        System.out.println("phonenumber: " + map.get("phonenumber").toString());
//        System.out.println("password: " + map.get("password").toString());
//        System.out.println("password: " + user.getPassword());

//      参数校验
        if (result.hasErrors()) {
            return resultMap.fail().code(401).message(result.getFieldError().getDefaultMessage());
        }

        if (userMapper.getPassword(user.getUsername()) == null) {
            return resultMap.fail().code(401).message("该手机号未注册").token("").res("").data(null);
        }
        String realPassword = userMapper.getPassword(user.getUsername());

        if (!realPassword.equals(DigestUtils.md5DigestAsHex(user.getPassword().getBytes())+ DigestUtils.md5DigestAsHex(user.getUsername().getBytes()))) {
            return resultMap.fail().code(401).message("密码错误").token("").res("").data(null);
        } else {
            Map<String, String> userInfo = userMapper.getUserInfo(user.getUsername());

            return resultMap.success().code(200).message("登录成功 2 秒后跳转到首页").token(JWTUtil.createToken(user.getUsername())).res(userInfo).data(null);
        }
    }

//    @ApiOperation(value="用户登录", notes="手机号/密码")
//
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "phonenumber", value = "手机号", required = true ,dataType = "string"),
//            @ApiImplicitParam(name = "password", value = "密码", required = true ,dataType = "string")
//    })
//
//    @PostMapping("/login")
//    public ResultMap login(@Valid User user, BindingResult result, @RequestParam("phonenumber") String username,
//                           @RequestParam("password") String password) {

//        参数校验
//        if (result.hasErrors()) {
//            return resultMap.fail().code(401).message(result.getFieldError().getDefaultMessage());
//        }

//        String realPassword = userMapper.getPassword(username);

//        if (realPassword == null) {
//            return resultMap.fail().code(401).message("密码不能为空");
//        } else
//        if (!realPassword.equals(password)) {
//            return resultMap.fail().code(401).message("密码错误");
//        } else {
//            return resultMap.success().code(200).message(JWTUtil.createToken(username));
//        }
//    }

//    非法请求
    @RequestMapping(path = "/unauthorized/{message}")
    public ResultMap unauthorized(@PathVariable String message) throws UnsupportedEncodingException {
//        return resultMap.success().code(401).message(message);
        return resultMap.fail().code(401).message("没有权限").token("").res("").data(null);
    }
}
